import 'package:bookshelf/entity/classify_entity.dart';
import 'package:bookshelf/entity/pic_entity.dart';
import 'package:bookshelf/entity/rfid_entity.dart';
import 'package:bookshelf/entity/version_entity.dart';
import 'package:dio/dio.dart';

import '../entity/book_entity.dart';
import '../entity/borrow_again_entity.dart';
import '../entity/borrow_entity.dart';
import '../entity/face_search_entity.dart';
import '../entity/find_book_entity.dart';
import '../entity/reader_entity.dart';
import '../entity/return_entity.dart';
import '../main.dart';
import '../util/toast_util.dart';
import 'api_url.dart';
import 'http_helper.dart';


Future<List<RfidEntity>?> findRfidList() async {
  var (status,response) = await HttpHelper.instance.post(findRfidListUrl, data: {'device_id':settingEntity.deviceNo?.replaceAll('device_id=', '')},options: Options(receiveTimeout: Duration.zero));
  if(status == 200){
    if(response['status'] == 200 ){
      List<dynamic> data = response['data'];
      List<RfidEntity> list = [];
      if(data.isNotEmpty){
        for (var element in data) {
          var rfid = RfidEntity.fromJson(element);
          list.add(rfid);
        }
        return list;
      }

      return null;
    }
    toastWarn('${response['error']}');
    return null;
  }else{
    toastWarn("${response['error']}");
    return null;
  }
}

Future<List<BookEntity>?> queryBook(region,page,{String classify = '',String keyword = ''}) async {
  var (status,response) = await HttpHelper.instance.post(queryUrl, data: {'device_id':settingEntity.deviceNo?.replaceAll('device_id=', ''),"region": region,"page":page,"chi_classify":classify,"keywords":keyword},options: Options(receiveTimeout: Duration.zero));
  if(status == 200){
    if(response['status'] == 200 ){
      List<dynamic> data = response['data']['list'];
      List<BookEntity> list = [];
      if(data.isNotEmpty){
        for (var element in data) {
          var rfid = BookEntity.fromJson(element);
          list.add(rfid);
        }
        return list;
      }

      return null;
    }
    toastWarn('${response['error']}');
    return null;
  }else{
    toastWarn("${response['error']}");
    return null;
  }
}
Future<List<BookEntity>?> hot() async {
  var (status,response) = await HttpHelper.instance.post(hotUrl, data: {'device_id':settingEntity.deviceNo?.replaceAll('device_id=', '')},options: Options(receiveTimeout: Duration.zero));
  if(status == 200){
    if(response['status'] == 200 ){
      List<dynamic> data = response['data'];
      List<BookEntity> list = [];
      if(data.isNotEmpty){
        for (var element in data) {
          var rfid = BookEntity.fromJson(element);
          list.add(rfid);
        }
        return list;
      }

      return null;
    }
    toastWarn('${response['error']}');
    return null;
  }else{
    toastWarn("${response['error']}");
    return null;
  }
}
Future<List<BookEntity>?> recommend() async {
  var (status,response) = await HttpHelper.instance.post(recommendUrl, data: {'device_id':settingEntity.deviceNo?.replaceAll('device_id=', '')},options: Options(receiveTimeout: Duration.zero));
  if(status == 200){
    if(response['status'] == 200 ){
      List<dynamic> data = response['data'];
      List<BookEntity> list = [];
      if(data.isNotEmpty){
        for (var element in data) {
          var rfid = BookEntity.fromJson(element);
          list.add(rfid);
        }
        return list;
      }

      return null;
    }
    toastWarn('${response['error']}');
    return null;
  }else{
    toastWarn("${response['error']}");
    return null;
  }
}


Future<FindBookEntity?> findBook(bookNo) async {

  var (status,response) = await HttpHelper.instance.post(findBookUrl, data: {'more': settingEntity.deviceNo, 'book_no': bookNo});
  if(status == 200){
    if(response['status'] == 200){
      var data  = FindBookEntity.fromJson(response['data']);
      return data;
    }
    toastWarn('${response['error']}');
    return null;
  }else{
    toastWarn("${response['error']}");
    return null;
  }
}
Future<List<BookEntity>?> findList(book_id) async {

  var (status,response) = await HttpHelper.instance.post(findListUrl, data: {'device_id':settingEntity.deviceNo?.replaceAll('device_id=', ''), 'book_id': book_id});
  if(status == 200){
    if(response['status'] == 200 ){
      List<dynamic> data = response['data'];
      List<BookEntity> list = [];
      if(data.isNotEmpty){
        for (var element in data) {
          var rfid = BookEntity.fromJson(element);
          list.add(rfid);
        }
        return list;
      }

      return null;
    }
    toastWarn('${response['error']}');
    return null;
  }else{
    toastWarn("${response['error']}");
    return null;
  }
}


Future<List<ClassifyEntity>?> chiclassify() async {

  var (status,response) = await HttpHelper.instance.post(chiclassifyUrl, data: {'device_id':settingEntity.deviceNo?.replaceAll('device_id=', '')});
  if(status == 200){
    if(response['status'] == 200 ){
      List<dynamic> data = response['data'];
      List<ClassifyEntity> list = [];
      if(data.isNotEmpty){
        for (var element in data) {
          var c = ClassifyEntity.fromJson(element);
          list.add(c);
        }
        return list;
      }

      return null;
    }
    toastWarn('${response['error']}');
    return null;
  }else{
    toastWarn("${response['error']}");
    return null;
  }
}

Future init(list) async {
  var (status,response) = await HttpHelper.instance.post(initUrl, data: {'device_id':settingEntity.deviceNo?.replaceAll('device_id=', ''),'books':list});
  if(status == 200){
    if(response['status'] == 200){
      return true;
    }
    toastWarn('${response['error']}');
    return false;
  }else{
    toastWarn('${response['error']}');
    return false;
  }
}
Future advise(title,reason) async {
  var (status,response) = await HttpHelper.instance.post(adviseUrl, data: {'device_id':settingEntity.deviceNo?.replaceAll('device_id=', ''), "title": title,"reason":reason});
  if(status == 200){
    if(response['status'] == 200){
      return true;
    }
    toastWarn('${response['error']}');
    return false;
  }else{
    toastWarn('${response['error']}');
    return false;
  }
}

Future setting(config) async {
  var (status,response)  = await HttpHelper.instance.post(settingUrl, data: {'device_id':settingEntity.deviceNo?.replaceAll('device_id=', ''),'config':config},options: Options(receiveTimeout: const Duration(seconds: 10)));
  if(status == 200){
    if(response['status'] == 200){
      return true;
    }
    toastWarn('${response['error']}');
    return false;
  }else{
    toastWarn('${response['error']}');
    return false;
  }
}
Future getSetting() async {
  var (status,response)  = await HttpHelper.instance.post(getSettingUrl, data: {'device_id':settingEntity.deviceNo?.replaceAll('device_id=', '')},options: Options(receiveTimeout: const Duration(seconds: 10)));
  if(status == 200){
    if(response['status'] == 200){
      return response['data'];
    }
    toastWarn('${response['error']}');
    return false;
  }else{
    toastWarn('${response['error']}');
    return false;
  }
}
Future inventory(books) async {
  var (status,response)  = await HttpHelper.instance.post(inventoryUrl, data: {'device_id':settingEntity.deviceNo?.replaceAll('device_id=', ''),'books':books},options: Options(receiveTimeout: const Duration(seconds: 10)));
  if(status == 200){
    if(response['status'] == 200){
      return true;
    }
    toastWarn('${response['error']}');
    return false;
  }else{
    toastWarn('${response['error']}');
    return false;
  }
}
Future operate(books) async {
  var (status,response)  = await HttpHelper.instance.post(operateUrl, data: {'device_id':settingEntity.deviceNo?.replaceAll('device_id=', ''),'books':books},options: Options(receiveTimeout: const Duration(seconds: 10)));
  if(status == 200){
    if(response['status'] == 200){
      return true;
    }
    toastWarn('${response['error']}');
    return false;
  }else{
    toastWarn('${response['error']}');
    return false;
  }
}

Future addScore(books) async {
  var (status,response)  = await HttpHelper.instance.post(addScoreUrl, data: {'device_id':settingEntity.deviceNo?.replaceAll('device_id=', ''),'books':books},options: Options(receiveTimeout: const Duration(seconds: 10)));
  if(status == 200){
    if(response['status'] == 200){
      return true;
    }
    toastWarn('${response['error']}');
    return false;
  }else{
    toastWarn('${response['error']}');
    return false;
  }
}

Future bind(reader_no,data) async {
  var (status,response)  = await HttpHelper.instance.post(bindUrl, data: {'device_id':settingEntity.deviceNo?.replaceAll('device_id=', ''),'reader_no':reader_no,"category":"icr_uid","data":data},options: Options(receiveTimeout: const Duration(seconds: 10)));
  if(status == 200){
    if(response['status'] == 200){
      return true;
    }
    toastWarn('${response['error']}');
    return false;
  }else{
    toastWarn('${response['error']}');
    return false;
  }
}
Future findBind(data) async {
  var (status,response)  = await HttpHelper.instance.post(findBindUrl, data: {'device_id':settingEntity.deviceNo?.replaceAll('device_id=', ''),'data':data,"category": "icr_uid",},options: Options(receiveTimeout: const Duration(seconds: 10)));
  if(status == 200){
    if(response['status'] == 200){
      return response['data']['reader_no'];
    }
    toastWarn('${response['error']}');
    return null;
  }else{
    toastWarn('${response['error']}');
    return null;
  }
}

Future version() async {
  var (status,response)  = await HttpHelper.instance.post(versionUrl, data: {'device_id':settingEntity.deviceNo?.replaceAll('device_id=', ''), "app_id": "Bookshelf.Android"},options: Options(receiveTimeout: const Duration(seconds: 10)));
  if(status == 200){
    if(response['status'] == 200){
      return VersionEntity.fromJson(response['data']);
    }
    toastWarn('${response['error']}');
    return null;
  }else{
    toastWarn('${response['error']}');
    return null;
  }
}
Future<List<PicEntity>?> asset(kind) async {
  var (status,response)  = await HttpHelper.instance.post(assetUrl, data: {'device_id':settingEntity.deviceNo?.replaceAll('device_id=', ''), "kind": kind},options: Options(receiveTimeout: const Duration(seconds: 10)));
  if(status == 200){
    if(response['status'] == 200){
      List<dynamic> str = response['data'];
      List<PicEntity> listData = [];
      for (var element in str) {
        listData.add(PicEntity.fromJson(element));
      }
      return listData;
    }
    toastWarn('${response['error']}');
    return null;
  }else{
    toastWarn('${response['error']}');
    return null;
  }
}

Future<ReaderEntity?> findReader(readerNo, {String category = ''}) async {
  var (status,response) = await HttpHelper.instance.post(findReaderUrl, data: {'more': settingEntity.deviceNo, 'reader_no': readerNo,'password':'000000','category':category});

  if(status == 200){
    if(response['status'] == 200){
      var data  = ReaderEntity.fromJson(response['data']);
      return data;
    }
    toastWarn('${response['error']}');
    return null;
  }else{
    toastWarn("${response['error']}");
    return null;
  }
}


Future bookBorrow(readerNo,bookNo) async {
  var (status,response) = await HttpHelper.instance.post(borrowUrl, data: {'more': settingEntity.deviceNo, 'reader_no': readerNo,'book_no':bookNo});

  if(status == 200){
    if(response['status'] == 200){
      var data  = BorrowEntity.fromJson(response['data']);
      return data;
    }
    toastWarn("${response['error']}");
    return null;
  }else {
    toastWarn("${response['error']}");
    return null;
  }

}


Future bookReturn(bookNo) async {
  var (status,response) = await HttpHelper.instance.post(returnUrl, data: {'more': settingEntity.deviceNo,'book_no':bookNo});
  if(status == 200){
    if(response['status'] == 200){
      var data  = ReturnEntity.fromJson(response['data']);
      return data;
    }
    toastWarn("${response['error']}");
    return null;
  }else {
    toastWarn("${response['error']}");
    return null;
  }
}

Future bookBorrowAgain(readerNo,bookNo) async {
  var (status,response) = await HttpHelper.instance.post(borrowAgainUrl, data: {'more': settingEntity.deviceNo, 'reader_no': readerNo,'book_no':bookNo});
  if(status == 200){
    if(response['status'] == 200){
      var data  = BorrowAgainEntity.fromJson(response['data']);
      return (200,data);
    }
    return (400,response['error']);
  }else {
    return (400, response['error']);
  }
}


Future faceAdd(userId,userName,img) async {
  var (status,response) = await HttpHelper.instance.oldPost(faceAddUrl, data: {'device_id':settingEntity.deviceNo?.replaceAll('device_id=', ''),'user_id': userId, 'user_name': userName,'image_base64':img},options: Options(receiveTimeout: const Duration(seconds: 10)));
  if(status == 200){
    return (true,null);
  }else{
    return (false, response['error']);
  }

}

Future faceSearch(img) async {
  var (status,response) = await HttpHelper.instance.oldPost(faceSearchUrl, data: {'device_id':settingEntity.deviceNo?.replaceAll('device_id=', img),'image_base64':''},options: Options(receiveTimeout: const Duration(seconds: 10)));
  if(status == 200){
    var data  = FaceSearchEntity.fromJson(response);
    return (data,null);
  }else{
    return (null, response['error']);
  }

}
Future<bool> faceQuery(userId) async {
  var (status,response) = await HttpHelper.instance.oldPost(faceQueryUrl, data: {'device_id':settingEntity.deviceNo?.replaceAll('device_id=', ''),"user_id": userId},options: Options(receiveTimeout: const Duration(seconds: 10)));
  if(status == 200){
    bool isEmpty  = response['empty'];
    return !isEmpty;
  }else{
    // toastWarn('${response['error']}');
    return false;
  }

}

